package main

import (
	"log"
	"sync"
)

func main() {
	log.SetFlags(0)

	x := 0
	dosomething := func() {
		x++
		log.Println("hello")

	}

	var wg sync.WaitGroup
	var once sync.Once

	for i := 0; i < 5; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			once.Do(dosomething) // 此函数只会被执行一次
			log.Println("world")

		}()
	}
	wg.Wait()
	log.Println(x)

}

/*
hello
world
world
world
world
world
1

*/
